package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func DictSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectDictQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectDict(&selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func DictFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectDictQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindDict(&selectQueryParam.Filter)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func DictCreate(ctx *gin.Context) {
	var addDicts validate.AddDictWrapper

	err := ctx.ShouldBindJSON(&addDicts)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateDict(addDicts.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	} else {
		// 插入数据成功
		addDicts.Data.Id = resData.Id
	}

	response.Success(ctx, gin.H{"data": addDicts.Data}, "success")
}

func DictUpdate(ctx *gin.Context) {
	var updateDict validate.UpdateDictWrapper

	err := ctx.ShouldBindJSON(&updateDict)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateDict(updateDict.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	} else {
		// 插入数据成功
		updateDict.Data.Id = resData.Id
	}

	response.Success(ctx, gin.H{"data": updateDict.Data}, "success")
}

// 删除词库
func DictDelete(ctx *gin.Context) {
	var deleteDict validate.DeleteDictParam

	err := ctx.ShouldBindJSON(&deleteDict)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	deleteFilterParam := common.GenerateFilterParam(&deleteDict.Param, "dict")

	service.DeleteDict(&deleteFilterParam)

	response.Success(ctx, nil, "success")
}

func DictPull(ctx *gin.Context) {
	var pullDict validate.PullDict

	err := ctx.ShouldBindJSON(&pullDict)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	err = service.PullDict(&pullDict)

	if err != nil {
		// 插入失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, nil, "success")
}

func DictAddByEntityCustomTags(ctx *gin.Context) {
	var dictAddByEntityCustomTags validate.DictAddByEntityCustomTags

	err := ctx.ShouldBindJSON(&dictAddByEntityCustomTags)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	res, err := service.DictAddByEntityCustomTags(&dictAddByEntityCustomTags)

	if err != nil {
		// 插入失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, gin.H{"data": res}, "success")
}
